/*
 * 版权所有(c)  1952110078@qq.com  2022 - 2022
 */

package com.ldc.module_main.viewmodel;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ldc.commonlibs.common.RspListModel;
import com.ldc.commonlibs.common.ViewModelBase;
import com.ldc.commonlibs.conatants.ParamsConstants;
import com.ldc.commonlibs.http.ApiService;
import com.ldc.commonlibs.http.NetCallback;
import com.ldc.commonlibs.http.RetrofitClient;
import com.ldc.commonlibs.utils.JsonUtil;
import com.ldc.module_main.model.BannerModel;
import com.ldc.module_main.model.DragonBollModel;
import com.ldc.module_main.model.PersonalizedModel;
import com.ldc.module_main.net.HttpService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liudc
 * @since 2022/7/19 23:38
 */
public class BannerCarViewModel extends ViewModelBase {
    private final HttpService apiService = RetrofitClient.getInstance().getService(HttpService.class);
    private final MutableLiveData<RspListModel<DragonBollModel>> _dragonBoll = new MutableLiveData<>();
    private final MutableLiveData<List<BannerModel>> _bannerList = new MutableLiveData<>();
    private final MutableLiveData<PersonalizedModel> _personalizedModel = new MutableLiveData<>();

    public MutableLiveData<RspListModel<DragonBollModel>> getDragonBoll() {
        return _dragonBoll;
    }

    public MutableLiveData<List<BannerModel>> getBannerList() {
        return _bannerList;
    }

    public MutableLiveData<PersonalizedModel> getPersonalizedModel() {
        return _personalizedModel;
    }

    public void queryDragonBoll() {
        getNetDataSource(apiService.dragonBoll(), new NetCallback<RspListModel<DragonBollModel>>() {
            @Override
            public void onReceiver(RspListModel<DragonBollModel> data) {
                _dragonBoll.setValue(data);
            }

            @Override
            public void onBadReceiver(Throwable throwable) {
                _dragonBoll.setValue(null);
            }
        });
    }

    public void getBanner() {
        getNetDataSource(apiService.banner(), new NetCallback<JsonObject>() {
            @Override
            public void onReceiver(JsonObject data) {
                if (!data.has(ParamsConstants.BANNERS)) {
                    _bannerList.setValue(null);
                    return;
                }
                JsonArray jsonArray = data.get(ParamsConstants.BANNERS).getAsJsonArray();
                List<BannerModel> bannerModelList = new ArrayList<>(jsonArray.size());
                for (JsonElement item : jsonArray) {
                    BannerModel bannerModel = JsonUtil.jsonToModel(item.toString(), BannerModel.class);
                    bannerModelList.add(bannerModel);
                }
                _bannerList.setValue(bannerModelList);

            }

            @Override
            public void onBadReceiver(Throwable throwable) {
                _bannerList.setValue(null);
            }
        });
    }

    public void personalized() {
        getNetDataSource(apiService.personalized(), new NetCallback<PersonalizedModel>() {
            @Override
            public void onReceiver(PersonalizedModel data) {
                _personalizedModel.setValue(data);
            }

            @Override
            public void onBadReceiver(Throwable throwable) {
                _personalizedModel.setValue(null);
            }
        });
    }
}
